
#include "para.hpp"
#include "../../xcom/xrb.hpp"

namespace rb_test
{
    
    rb g_rb;

    uint64_t write_cost_avg = 0;
    uint64_t read_cost_avg = 0;

    static void write_thread()
    {
        auto begin = get_ns();
        for(auto i = 0;i < test_cnt;++i){
            while(-1 == g_rb.writer_get_idx())
                ;

            g_rb.writer_done(1);
        }
        
        write_cost_avg = (get_ns() - begin)/test_cnt;
    }

    static void read_thread()
    {
        auto begin = get_ns();

        for(auto i = 0;i < test_cnt;++i){
            while(-1 == g_rb.reader_get_idx())
                ;
            g_rb.reader_done(1);
        }

        read_cost_avg = (get_ns() - begin)/test_cnt;
    }

    static int  test()
    {
        g_rb.init(1,16);

        std::thread t1(write_thread);
        if(-1 != cpu_bind1)
        {
            auto bind_ret = cpu_bind(t1.native_handle(),cpu_bind1);
            X_CHECK(bind_ret,-1);
        }

        std::thread t2(read_thread);
        if(-1 != cpu_bind2)
        {
            
            auto bind_ret2 = cpu_bind(t2.native_handle(),cpu_bind2);
            X_CHECK(bind_ret2,-1);
        }

        t1.join();
        t2.join();

        printf("cpu1=%d,cpu2=%d,write_cost_avg=%ju,read_cost_avg=%ju\n",
            cpu_bind1,cpu_bind2,write_cost_avg,read_cost_avg);
        return 0;
    }
}